﻿using System;
using System.Configuration;

namespace RSPDS.OAuth
{
    public abstract class RSPDS_OAuthBaseClient : IRSPDS_OAuthClient
    {
        #region Properties

        public string ConsumerKey { get; private set; }

        public string ConsumerSecret { get; private set; }

        public string LoginURI { get; private set; }

        public string RedirectURI { get; private set; }

        public string TokenRequestURI { get; private set; }

        #endregion Properties

        #region Construction

        public static IRSPDS_OAuthClient Create<T>() where T : RSPDS_OAuthBaseClient, new()
        {
            var result = new T();
            // Load the result into the new object.
            var config = ConfigurationManager.AppSettings;
            result.ConsumerKey = config["ConsumerKey"];
            result.ConsumerSecret = config["ConsumerSecret"];
            result.LoginURI = config["LoginURI"];
            result.RedirectURI = config["RedirectURI"];
            result.TokenRequestURI = config["TokenURI"];
            return result;
        }

        #endregion Construction

        #region Methods

        public Uri ResolveLoginURI()
        {
            if (String.IsNullOrEmpty(ConsumerKey)) throw new ArgumentException("Consumer Key is empty");
            if (String.IsNullOrEmpty(ConsumerSecret)) throw new ArgumentException("Consumer Secret is empty");
            if (String.IsNullOrEmpty(LoginURI)) throw new ArgumentException("Login URI is empty");
            return new Uri(LoginURI.Replace("CLIENT_ID", ConsumerKey).Replace("REDIRECT_URI", RedirectURI));
        }

        #endregion Methods
    }
}
